package com.lemon.opencv;




import org.junit.Test;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class MyOpenCV{

	@Test
	public void test(){
//		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
		System.loadLibrary("opencv_java340");
        CascadeClassifier faceDetector = new CascadeClassifier("D:/software/opencv/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml");
        Mat image = Imgcodecs.imread("d:/wechart.jpg");
        if(image.empty())
        	System.out.println("一张脸都没找到");
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);

        System.out.println(String.format("识别到 %s 张脸",faceDetections.toArray().length));
        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x
              + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
        }

        String filename = "d:/result.png";
        Imgcodecs.imwrite(filename, image);
	}
}
